<template>
  <div class="app-wrapper">
    <div :class="{ 'fixed-header': $store.state.settings.fixed_header }">
      <navbar />
    </div>
    <div class="flex">
      <sidebar :aMenu="adminMenu" style="z-index: 9" v-if="menuState" />
      <app-main v-if="adminMenu.length > 0" />
    </div>
  </div>
</template>
<script>
import Navbar from "./component/navbar";
import Sidebar from "@/layout/components/sidebar/adminNew";
import AppMain from "@/layout/components/appmain/adminNew";

import { getAreas } from "./api";

export default {
  components: {
    Navbar,
    Sidebar,
    AppMain
  },
  data() {
    return {
      menuState: !this.$route.query.app_auth_code
    };
  },
  watch: {
    $route(to) {
      if (to.meta?.route) {
        sessionStorage.setItem("taxCalculatePage", to.meta.route);
      }
    }
  },
  computed: {
    adminMenu() {
      const arr = [];
      if (this.$hasPermissionSecond("zhinengsuanshui:koujiaoyiwurenguanli")) {
        arr.push({
          icon: "",
          name: "扣缴义务人管理",
          router: "/taxCalculate/management"
        });
      }
      if (this.$hasPermissionSecond("zhinengsuanshui:baoshuirenyuanku")) {
        arr.push({
          icon: "",
          name: "个税人员库",
          router: "/taxCalculate/people"
        });
      }
      if (this.$hasPermissionSecond("zhinengsuanshui:renyuanbaosong.daibaosong")) {
        const children = [];
        children.push({
          icon: "",
          name: "待报送",
          router: "/taxCalculate/waiting"
        });
        children.push({
          icon: "",
          name: "已报送",
          router: "/taxCalculate/report"
        });
        children.push({
          icon: "",
          name: "停止报送",
          router: "/taxCalculate/stop"
        });
        arr.push({
          icon: "",
          name: "人员报送",
          router: "/taxCalculate",
          children
        });
      }
      if (this.$hasPermissionSecond("zhinengsuanshui:geshuijisuan")) {
        arr.push({
          icon: "",
          name: "个税计算",
          router: "/taxCalculate/calculate"
        });
      }
      return arr;
    }
  },
  created() {
    this.onGetAreas();
  },
  methods: {
    async onGetAreas() {
      try {
        const data1 = await getAreas(1);
        const data2 = await getAreas(2);
        const arr = JSON.parse(JSON.stringify(data1.data));
        arr.forEach((e) => {
          e.children = [];
          data2.data.forEach((s) => {
            if (e.code === s.parentCode) {
              e.children.push(s);
            }
          });
        });
        this.$store.commit("taxCalculate/cityTreeSelectOption", arr);
      } catch (error) {
        console.log("debug log --> ", error);
      }
    }
  },
  destroyed() {
    sessionStorage.setItem("taxCalculatePage", "");
  }
};
</script>
